package com.hcb.model.utils;
import com.hcb.common.ActionEvent;
import com.hcb.constants.Constants;
import com.hcb.dto.CustomerDTO;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class CustomerModel {
	public static CustomerModel instance;

	public static CustomerModel getInstance() {
		if (instance == null)
			instance = new CustomerModel();
		return instance;
	}

	public void handleControllerEvent(ActionEvent e) {

		switch (e.action) {
		case Constants.INSERT_CUSTOMER:
			CustomerDTO dto = (CustomerDTO) e.viewData;
			insertCustomer(dto, (Context) e.sender);
			break;

		case Constants.UPDATE_CUSTOMER:
			CustomerDTO dto1 = (CustomerDTO) e.viewData;
			updateCustomer(dto1, (Context) e.sender);
			break;
		case Constants.INSERT_CUSTOMER_EXIST:
			CustomerDTO dtoexits = (CustomerDTO) e.viewData;
			insertCustomerEXITS(dtoexits, (Context) e.sender);
			break;
		default:
			break;
		}
	}

	public int getFlagCustomer(Context context) {
		int id = 0;
		String sql = "SELECT * FROM customer";
		DBHelper db = new DBHelper(context);
		Cursor c = db.Excutequery(sql);

		id = c.moveToLast() == true ? c.getInt(c.getColumnIndex("flag")) : 0;
		c.close();
		db.close();
		return id;
	}

	public boolean insertCustomer(CustomerDTO dto, Context context) {
		boolean flag = false;
		String DATABASE_TABLE = Constants.TABLE_CUSTOMER;
		ContentValues values = new ContentValues();

		values.put("customer_id", dto.getCustomer_id());
		values.put("mobile_number", dto.getMobile_number());
		values.put("name", dto.getName());
		values.put("email_address", dto.getEmail_address());
		values.put("age", dto.getAge());
		values.put("sex", dto.getSex());
		values.put("flag", 1);
		DBHelper db = DBHelper.getInstance(context);
		flag = db.ExcuteInsert(null, DATABASE_TABLE, values);
		db.close();
		return flag;

	}

	public boolean insertCustomerEXITS(CustomerDTO dto, Context context) {
		boolean flag = false;
		String DATABASE_TABLE = Constants.TABLE_CUSTOMER;
		ContentValues values = new ContentValues();

		values.put("customer_id", dto.getCustomer_id());
		values.put("mobile_number", dto.getMobile_number());
		values.put("name", dto.getName());
		values.put("email_address", dto.getEmail_address());
		values.put("age", dto.getAge());
		values.put("sex", dto.getSex());
		values.put("flag", 2);
		DBHelper db = DBHelper.getInstance(context);
		flag = db.ExcuteInsert(null, DATABASE_TABLE, values);
		db.close();
		return flag;

	}

	public boolean updateCustomer(CustomerDTO dto, Context context) {
		boolean flag = false;
		String DATABASE_TABLE = Constants.TABLE_CUSTOMER;
		String where = "customer_id= " + dto.getCustomer_id() + "";
		ContentValues values = new ContentValues();
		values.put("mobile_number", dto.getMobile_number());
		values.put("name", dto.getName());
		values.put("email_address", dto.getEmail_address());
		values.put("age", dto.getAge());
		values.put("sex", dto.getSex());
		values.put("flag", 2);
		DBHelper db = DBHelper.getInstance(context);
		flag = db.ExcuteUpdate(null, DATABASE_TABLE, values, where, null);
		db.close();
		return flag;
	}

	public CustomerDTO getCustomer(Context context) {
		CustomerDTO cus = new CustomerDTO();
		String sql = "SELECT * FROM customer";
		DBHelper db = new DBHelper(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			cus.setCustomer_id(c.getString(c.getColumnIndex("customer_id")));
			cus.setMobile_number(c.getString(c.getColumnIndex("mobile_number")));
			cus.setName(c.getString(c.getColumnIndex("name")));
			cus.setEmail_address(c.getString(c.getColumnIndex("email_address")));
			cus.setAge(c.getString(c.getColumnIndex("age")));
			cus.setSex(c.getString(c.getColumnIndex("sex")));

		}
		c.close();
		db.close();
		return cus;
	}
}
